<template>
    <div class="login_container">
        <div class="login_main">
            <div class="l_m_center">
                <div class="header">
                    <img src="@/static/images/userLogin.png" alt="">
                </div>
                <div class="login_form">
                    <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="70px"
                             class="demo-dynamic" :rules="rules" :label-position="'left'">
                        <div class="form_item">
                            <el-form-item prop="phone" label="手机号*">
                                <el-input v-model="dynamicValidateForm.phone" placeholder="phone"></el-input>
                            </el-form-item>
                        </div>
                        <div class="form_item">
                            <el-form-item label="密码*" prop="pass" class="pwd_item">
                                <el-input type="password" v-model="dynamicValidateForm.pass" autocomplete="off"
                                          placeholder="password" v-if="!isShowPwd">
                                </el-input>
                                <el-input type="text" v-model="dynamicValidateForm.pass" autocomplete="off"
                                          placeholder="password" v-else>
                                </el-input>
                                <span class="showSwitch" @click="showPwd">
                      <svg-icon :iconClass="isShowPwd?'eye2':'eye'"></svg-icon>
                    </span>
                            </el-form-item>
                            <router-link to="/register" tag="p" class="store_register">免费注册</router-link>
                        </div>
                    </el-form>
                    <div class="btn_container">
                        <el-button type="primary" @click="submitForm('dynamicValidateForm')">
                            登录</el-button>
                    </div>
                    <div class="agree">
                        <p>登录即同意并遵守《巴拉巴拉协议》</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import '@/icons/svg/form.svg'
    import {getUserByPhone} from "@/api/user";
    export default {
        name: 'login',
        data() {
            const validateName = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('用户名不能为空'));
                }
                return callback();
            };
            const validatePass = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('密码不能为空'));
                }
                return callback();
            };
            return {
                dynamicValidateForm: {
                    phone: '18600010000',
                    pass: '123456',
                },
                isShowPwd: false,
                rules: {
                    phone: [
                        { validator: validateName, trigger: 'blur' },
                    ],
                    pass: [
                        { validator: validatePass, trigger: 'blur' },
                    ],
                },
            };
        },
        rules: {},
        methods: {
            async submitForm() {
                /*this.axios.post('api/cgi/manager/login', {
                    "mobile": this.dynamicValidateForm.phone,
                    "password": this.dynamicValidateForm.pass,
                }).then((res) => {
                    if (res.data.code === 200) {
                        res.data.body.manager.pass = this.dynamicValidateForm.pass;
                        console.log(res.data.body.manager);
                        this.$store.dispatch('setStorage', res.data.body.manager);
                        this.$router.push('/dashboard');
                    }
                });*/
                if (this.dynamicValidateForm.phone === '18600010000' && this.dynamicValidateForm.pass === '123456') {
                    this.$message({
                        type: 'error',
                        message: '账号密码能给你搁脸上？还好意思直接登录？'
                    });
                } else {
                    var user = (await getUserByPhone(this.dynamicValidateForm.phone)).data[0];
                    if (typeof user !== undefined && typeof user != "undefined") {
                        if (user.user_password === this.dynamicValidateForm.pass) {
                            var millisecond = new Date().getTime();
                            var expiresTime = new Date(millisecond + 60 * 1000 * 60);

                            this.$cookie.set('user_id', user.user_id, {
                                expires: expiresTime,
                            });
                            this.$cookie.set('user_name', user.user_name, {
                                expires: expiresTime,
                            });
                            this.$router.push('/dashboard');
                        }
                    }else {
                        this.$message({
                            type: 'error',
                            message: '该手机号未注册使用'
                        });
                    }

                    //this.$store.dispatch('setStorage', '这个res.data.body.manager');


                }
            },
            showPwd() {
                if (this.dynamicValidateForm.pass === '') {
                    console.log('密码不能为空');
                    return;
                }
                this.isShowPwd = !this.isShowPwd;
            },
        },
        components: {

        },
        mounted() {
            /*if (this.$store.getters.getStorage) {
                this.$router.push('/dashboard');
            }*/
        },
    };
</script>

<style scoped>
    .login_container{
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
    }
    .login_main{
        width: 100%;
    }
    .l_m_center{
        width: 600px;
        margin: 80px auto;
        border: 1px solid #ccc;
        border-radius: 6px;
        padding-bottom: 60px;
    }
    .header{
        text-align: center;
        border-bottom: 1px solid #eee;
        padding: 20px 0;
    }
    .login_form{
        padding: 40px 38px 40px;
    }
    .btn_container{
        text-align: center;
    }
    .el-button{
        width: 35%;
    }
    .pwd_item{
        margin-bottom: 0;
    }
    .showSwitch{
        position: absolute;
        right: 10px;
        top: 0;
        cursor: pointer;
    }
    .store_register{
        line-height: 14px;
        font-size: 12px;
        text-align: right;
        margin-top: 20px;
        cursor: pointer;
    }
    .agree{
        text-align: center;
        line-height: 40px;
    }
</style>
